package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.mapper.TPartnerMapper;
import com.ruoyi.system.mapper.TStationAccountMapper;
import com.ruoyi.system.mapper.TStationMapper;
import com.ruoyi.system.mapper.TWithdrawalRecordMapper;
import com.ruoyi.system.domain.TPartner;
import com.ruoyi.system.domain.TStation;
import com.ruoyi.system.domain.TStationAccount;
import com.ruoyi.system.domain.TWithdrawalRecord;
import com.ruoyi.system.service.ITWithdrawalRecordService;

/**
 * 提现记录Service业务层处理
 * 
 * @author xiuddoo
 * @date 2025-09-17
 */
@Service
public class TWithdrawalRecordServiceImpl implements ITWithdrawalRecordService 
{
    @Autowired
    private TWithdrawalRecordMapper tWithdrawalRecordMapper;
    
    @Autowired
    private TPartnerMapper tPartnerMapper;
    
    @Autowired
    private TStationMapper tStationMapper;
    
    @Autowired
    private TStationAccountMapper tStationAccountMapper;

    /**
     * 查询提现记录
     * 
     * @param id 提现记录主键
     * @return 提现记录
     */
    @Override
    public TWithdrawalRecord selectTWithdrawalRecordById(String id)
    {
        return tWithdrawalRecordMapper.selectTWithdrawalRecordById(id);
    }

    /**
     * 查询提现记录列表
     * 
     * @param tWithdrawalRecord 提现记录
     * @return 提现记录
     */
    @Override
    public List<TWithdrawalRecord> selectTWithdrawalRecordList(TWithdrawalRecord tWithdrawalRecord)
    {
    	List<TWithdrawalRecord> dataList = tWithdrawalRecordMapper.selectTWithdrawalRecordList(tWithdrawalRecord);
    	dataList.stream().forEach(item -> {
    		if(item.getApplicantType().equals("MERCHANT")) {
    			String name = "";
    			TStationAccount tStationAccount = tStationAccountMapper.selectTStationAccountById(item.getApplicantId());
    			
    			if(tStationAccount == null) {
    				name = "数据丢失";
    			}else {
    				name = tStationAccount.getRealName();
    				TStation tStation = tStationMapper.selectTStationById(tStationAccount.getStationId());
    				System.out.print(tStation);
    				if(tStation != null) {
    					name = tStation.getName()+" - "+name;
    				}
    			}
    			item.setApplicantId(name);
    		}
    		if(item.getApplicantType().equals("PARTNER")) {
    			TPartner tPartner = tPartnerMapper.selectTPartnerById(item.getApplicantId());
    			item.setApplicantId(tPartner.getRealName());
    		}
    	});
        return dataList;
    }

    /**
     * 新增提现记录
     * 
     * @param tWithdrawalRecord 提现记录
     * @return 结果
     */
    @Override
    public int insertTWithdrawalRecord(TWithdrawalRecord tWithdrawalRecord)
    {
        tWithdrawalRecord.setCreateTime(DateUtils.getNowDate());
        return tWithdrawalRecordMapper.insertTWithdrawalRecord(tWithdrawalRecord);
    }

    /**
     * 修改提现记录
     * 
     * @param tWithdrawalRecord 提现记录
     * @return 结果
     */
    @Override
    public int updateTWithdrawalRecord(TWithdrawalRecord tWithdrawalRecord)
    {
        tWithdrawalRecord.setUpdateTime(DateUtils.getNowDate());
        tWithdrawalRecord.setApproveTime(DateUtils.getNowDate());
        return tWithdrawalRecordMapper.updateTWithdrawalRecord(tWithdrawalRecord);
    }

    /**
     * 批量删除提现记录
     * 
     * @param ids 需要删除的提现记录主键
     * @return 结果
     */
    @Override
    public int deleteTWithdrawalRecordByIds(String[] ids)
    {
        return tWithdrawalRecordMapper.deleteTWithdrawalRecordByIds(ids);
    }

    /**
     * 删除提现记录信息
     * 
     * @param id 提现记录主键
     * @return 结果
     */
    @Override
    public int deleteTWithdrawalRecordById(String id)
    {
        return tWithdrawalRecordMapper.deleteTWithdrawalRecordById(id);
    }
    
    /**
     * 检查提现金额
     * 
     * @param tWithdrawalRecord 提现数据
     * @return 结果
     */
    @Override
    public boolean checkBalance(TWithdrawalRecord tWithdrawalRecord)
    {	
    	String accountId = "";
    	if(tWithdrawalRecord.getApplicantType().equals("MERCHANT")) {
    		TStationAccount tStationAccount = tStationAccountMapper.selectTStationAccountById(tWithdrawalRecord.getApplicantId());
			if(tStationAccount == null) {
				return false;
			}else {
				accountId = tStationAccount.getStationId();
			}
		}else if(tWithdrawalRecord.getApplicantType().equals("PARTNER")) {
			accountId = tWithdrawalRecord.getApplicantId();
		}else {
			return false;
		}
    	//已提现金额
    	float sum_amount = tWithdrawalRecordMapper.sumTWithdrawalRecordApprovedById(accountId,tWithdrawalRecord.getApplicantType(),tWithdrawalRecord.getId());
    	//总金额
    	float sum_amount_userget = tWithdrawalRecordMapper.sumTFundAllocationDetailById(accountId,tWithdrawalRecord.getApplicantType());
    	if(sum_amount_userget < sum_amount+tWithdrawalRecord.getAmount()) {
    		return false;
    	}
        return true;
    }
}
